"""
File: base_page.py
Author: ershixiong
Date: 2023/11/23
describe:

"""
import os.path
import time

import allure
import yaml
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    __BASE_URL = 'https://work.weixin.qq.com/wework_admin/frame#index'

    def __init__(self, driver=None):

        if driver:
            self.driver = driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(3)
            self.driver.maximize_window()

        if not self.driver.current_url.startswith('http'):
            self.driver.get(self.__BASE_URL)


    def goto_home(self,url):
        self.driver.get(url)

    def do_find(self, by, locator=None):
        if locator:
            return self.driver.find_element(by, locator)
        else:
            return self.driver.find_element(*by)

    def do_finds(self, by, locator=None):
        if locator:
            return self.driver.find_elements(by, locator)
        else:
            return self.driver.find_elements(*by)

    def do_click(self, by, locator=None):
        self.do_find(by, locator).click()

    def do_send(self, value, by, locator=None):
        ele = self.do_find(by, locator)
        ele.clear()
        ele.send_keys(value)

    def do_quit(self):
        self.driver.quit()

    def get_screen(self):
        timestamp = int(time.time())
        # images需要提前创建好
        image_path = f'../images/image_{timestamp}.PNG'
        # 截图
        self.driver.save_screenshot(image_path)
        # 添加图片到allure报告
        allure.attach.file(image_path, 'picture', attachment_type=allure.attachment_type.PNG)

    def wait_element_until_visible(self, locator: tuple):
        return WebDriverWait(self.driver, 10).until(
            expected_conditions.visibility_of_element_located(locator)
        )

    def wait_element_until_clickable(self, locator: tuple):
        return WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(locator)
        )

    def write_cookies_yaml(self, file_path):
        with open(file_path, 'w', encoding='utf-8') as f:
            time.sleep(30)
            yaml.safe_dump(self.driver.get_cookies(), f)

    def read_cookies_yaml(self, file_path):
        with open(file_path, 'r', encoding='utf-8') as f:
            cookies = yaml.safe_load(f)

        return cookies

    def do_set_cookies(self, file_path):
        # 判断cookie.yaml文件是否存在
        # 不存在，获取cookies，并保存到文件
        if not os.path.exists(file_path):
            self.write_cookies_yaml(file_path)

        # 存在，判断cookie有效期是否大于当前时间
        # 获取expiry
        expiry = int(self.read_cookies_yaml(file_path)[-1]['expiry'])

        now_timestamp = int(time.time())
        if expiry < now_timestamp:
            # 小于，先保存，然后读取并设置
            self.write_cookies_yaml()

        # 大于，直接读取并设置
        cookies = self.read_cookies_yaml(file_path)

        for cookie in cookies:
            # print(cookie)
            self.driver.add_cookie(cookie)
